<<!--用车申请页-->
<template>
  <div style="height: 6vh;background-color:#fff;padding:10px 20px;">
    <span style="line-height:60px;font-size:20px;">申请单列表</span>
    <el-button type="primary" style="float:right;margin-top:13px;" @click="addApplicationDialogVisible=true">申请用车</el-button>
  </div>
  <!-- 申请用车弹窗 -->
  <el-dialog title="创建申请单" v-model="addApplicationDialogVisible" style="width:1000px;padding:40px;"
  :before-close="cancelClose">
    <el-form label-width="80px" label-position="top">
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="用车人">
            <el-input readonly :value="user.username"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="使用时间">
            <el-date-picker
                type="datetimerange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                value-format="YYYY-MM-DD HH:mm:ss"
                format="YYYY-MM-DD HH:mm:ss"
                v-model = "times"
            ></el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="车辆出发地">
            <el-input placeholder="请输入" v-model="addApplicationForm.departureAddr"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="车辆目的地">
            <el-input placeholder="请输入" v-model="addApplicationForm.destinationAddr"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="用车事由">
            <el-input placeholder="请输入" type="textarea" resize="none" :rows="3" v-model="addApplicationForm.reason"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="备注">
            <el-input placeholder="请输入" type="textarea" resize="none" :rows="3" v-model="addApplicationForm.remark"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="驾照">
            <el-upload
                v-model:file-list="fileList"
                action="http://localhost:8080/v1/file/upload"
                name="file"
                limit="1"
                list-type="picture-card"
                :on-preview="handlePictureCardPreview"
                :on-remove="handleRemove"
            >
              <el-icon>
                <Plus/>
              </el-icon>
            </el-upload>
            <el-dialog v-model="dialogVisible">
              <img w-full :src="dialogImageUrl"/>
            </el-dialog>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="审批人">
            <el-row :gutter="10">
              <el-col :span="12" v-if="auditUsers.length>0">
                <el-tag type="primary">
                  <el-icon style="margin-right:5px;position:relative;top:2px;">
                    <User/>
                  </el-icon>
                  <span>{{auditUsers[0].username}}</span>
                </el-tag>
              </el-col>
              <el-col :span="12"  v-if="auditUsers.length>1">
                <el-tag type="primary">
                  <el-icon style="margin-right:5px;position:relative;top:2px;">
                    <User/>
                  </el-icon>
                  <span>{{auditUsers[1].username}}</span>
                </el-tag>
              </el-col>
            </el-row>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="cancelClose">取消</el-button>
      <el-button type="primary" @click="addApplication">确定</el-button>
    </template>
  </el-dialog>

  <!-- 用车申请列表 搜索卡片 -->
  <el-card style="margin: 20px;">
    <el-form style="padding-top:10px;">
      <el-row :gutter="30">
        <el-col :span="5">
          <el-form-item label="出发地">
            <el-input placeholder="请输入出发地" v-model="searchApplication.departureAddr"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item label="目的地">
            <el-input placeholder="请输入目的地" v-model="searchApplication.destinationAddr"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item label="申请单状态">
            <el-select placeholder="请选择" v-model="searchApplication.status">
              <el-option label="已发起" value="10"></el-option>
              <el-option label="撤销" value="20"></el-option>
              <el-option label="审核中" value="30"></el-option>
              <el-option label="驳回" value="40"></el-option>
              <el-option label="已通过" value="50"></el-option>
              <el-option label="分配用车" value="60"></el-option>
              <el-option label="工单结束" value="70"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item>
            <el-button @click="resetSearch">重置</el-button>
            <el-button type="primary" @click="loadApplication">查询</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </el-card>
  <!-- 用车申请表格 -->
  <el-card style="margin:20px">
    <el-table :data="applicationTableData">
      <el-table-column type="index" width="80" align="center" prop="id" label="编号"></el-table-column>
      <el-table-column align="center" prop="username" label="申请人"></el-table-column>
      <el-table-column align="center" prop="departureAddr" label="出发地"></el-table-column>
      <el-table-column align="center" prop="destinationAddr" label="目的地"></el-table-column>
      <el-table-column align="center" prop="reason" label="用车原因"></el-table-column>
      <el-table-column align="center" prop="auditUsernameList" label="审批人"></el-table-column>
      <el-table-column align="center" prop="startTime" label="使用开始时间"></el-table-column>
      <el-table-column align="center" prop="endTime" label="使用结束时间"></el-table-column>
      <el-table-column align="center" prop="status" label="申请单状态" :formatter="appStatusFormatter"></el-table-column>
      <el-table-column label="操作" width="120" align="center">
        <template #default="scope">
          <el-button type="primary" size="small" link
          :disabled="scope.row.status!=10" @click="cancel(scope.row.id)">撤销</el-button>
        </template>
      </el-table-column>
    </el-table>

  </el-card>
</template>
<script setup>
import {onMounted, ref} from "vue";
import axios from "axios";
import qs from "qs";
import {ElMessage} from "element-plus";
//定义变量控制创建申请单弹窗是否出现
const addApplicationDialogVisible = ref(false);
//图片上传以及预览和删除
const fileList = ref([]);
const dialogImageUrl = ref('')
const dialogVisible = ref(false)
const handlePictureCardPreview = (uploadFile)=>{
  dialogVisible.value = true;
  // console.log(uploadFile)
  dialogImageUrl.value = BASE_URL+uploadFile.response.data;
}
const handleRemove = (uploadFile,uploadFiles)=>{
  //调用后端代码进行删除，需要一个imgUrl
  let imgUrl = uploadFile.response.data;
  console.log("imgUrl"+imgUrl);
  axios.post(BASE_URL+"/v1/file/remove?imgUrl="+imgUrl).then((response)=>{
    if(response.data.code == 2000){
      ElMessage.success("删除成功")
    }else {
      ElMessage.error(response.data.msg)
    }
  })
}
//用车人
const user = ref(getUser());
//审批人列表
const auditUsers = ref([])
//定义个用于存储审批人id的对象
const auditUsersId = ref([])
//加载审批人的列表
const loadAuditUser = ()=>{
  console.log(user)
  //获取当前登陆人的parentId
  let parentId = user.value.parentId;

  //根据父Id查询上级领导信息(审批人)
  axios.get(BASE_URL+"/v1/user/select/audit/"+parentId).then((response)=>{
    if (response.data.code==2000){
      auditUsers.value = response.data.data;
      //将审批人的id存储起来，以供新增申请表单时使用
      for(let  i=0;i<auditUsers.value.length;i++){
        auditUsersId.value.push(auditUsers.value[i].id)
      }
    }
  })
}
//定义一个时间变量
const times = ref([])
//定义一个申请表单添加的对象
const addApplicationForm = ref({
  departureAddr:'', //出发地
  destinationAddr:'', //目的地
  reason:'',//用车事由
  remark:'' //备注
})
//新增申请表单
const addApplication=()=>{
  //检查图片是否上传
  if(fileList.value.length==0){
    ElMessage.error("请上传图片")
    return;
  }
  //准备要存入数据库的数据
  addApplicationForm.value.username = user.value.username;
  addApplicationForm.value.userId = user.value.id;
  addApplicationForm.value.startTime = times.value[0]; //开始时间
  addApplicationForm.value.endTime = times.value[1]; //结束时间
  addApplicationForm.value.imgUrl = fileList.value[0].response.data; //当前图片路径
  addApplicationForm.value.auditUserIdList = auditUsersId.value; //审批人的id
  console.log(addApplicationForm.value);
  let data = qs.stringify(addApplicationForm.value);
  axios.post(BASE_URL+"/v1/application/save",data).then((response)=>{
    if (response.data.code==2000){
      ElMessage.success("新增申请表成功");
      //关闭弹窗
      addApplicationDialogVisible.value = false;
      //清空表单数据
      addApplicationForm.value = {};
      times.value = [];
      fileList.value = [];
      loadApplication();
    }else {
      ElMessage.error(response.data.msg);
    }
  })
}
//处理取消的函数
const cancelClose = ()=>{
  if(confirm("您确定要取消吗？")){
    addApplicationDialogVisible.value = false;
    addApplicationForm.value = {};
    times.value = [];
    fileList.value = [];
  }
}
//定义查询申请表单的搜索条件对象
const searchApplication = ref({
  departureAddr:'', //出发地
  destinationAddr:'', //目的地
  status:''
})
//定义一个用于存储申请表单数据的数组对象
const applicationTableData = ref([]);
//加载申请表单数据
const loadApplication = ()=>{
  let data = qs.stringify(searchApplication.value);
  axios.get(BASE_URL+"/v1/application/select?"+data).then((response)=>{
    if (response.data.code==2000){
     applicationTableData.value = response.data.data;
    }else {
      ElMessage.error(response.data.msg);
    }
  })
}
//状态从数字项转为文本
const appStatusFormatter = (row,column,cellValue,index)=>{
  if(cellValue==10){
    cellValue="已发起";
  }else if(cellValue==20){
    cellValue="撤销";
  }else if(cellValue==30){
    cellValue="审核中";
  }else if(cellValue==40){
    cellValue="驳回";
  }else if(cellValue==50){
    cellValue="已通过";
  }else if(cellValue==60){
    cellValue="分配用车";
  }else if(cellValue==70){
    cellValue="工单结束";
  }
  return cellValue;
}
//撤销申请
const cancel = (id)=>{
  axios.post(BASE_URL+'/v1/application/cancel/'+id).then((response)=>{
    if (response.data.code==2000){
      ElMessage.success("已撤销申请单")
      //加载申请单的数据
      loadApplication();
    }else {
      ElMessage.error(response.data.msg);
    }
  })
}
onMounted(()=>{
  loadAuditUser();
  loadApplication();
})
</script>

<style scoped>
</style>